const execSync = require('child_process').execSync;
const exec = require('child_process').exec;
const schedule = require('node-schedule');

const job = schedule.scheduleJob('*/1 * * * *', function () {

    const phalaStatusRes = execSync('phala status').toString();
    // console.log(phalaStatusRes);
    const statusObj = {};
    const statusStrs = phalaStatusRes.split('\n');
    for (let str of statusStrs) {
        if (str.indexOf('phala') >= 0) {
            str = str.trim().replace('\t', ' ');
            const serviceStatus = str.split(/\s+/);
            statusObj[serviceStatus[0]] = serviceStatus[1];
        }
    }
    console.log(statusObj);

    for (let serviceName of Object.keys(statusObj)) {
        if (statusObj[serviceName] == 'stop') {
            console.log('重启' + serviceName);
            exec('phala start ' + serviceName);
        }
    }
});